package com.zbkj.service.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.dal.base.enums.BooleanEnums;
import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.dao.MerchantDao;
import com.zbkj.dal.dao.MerchantEmployeeDao;
import com.zbkj.dal.entity.merchant.Merchant;
import com.zbkj.dal.entity.merchant.MerchantEmployee;
import com.zbkj.service.manager.MerchantManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Component
public class MerchantManagerImpl implements MerchantManager {

    @Resource
    private MerchantDao merchantDao;

    @Resource
    private MerchantEmployeeDao merchantEmployeeDao;

    @Override
    public Merchant findByMerchantId(Integer merchantId) {
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getId, merchantId);
        wrapper.eq(Merchant::getIsDel, BooleanEnums.FALSE.getCode());
        Merchant merchant = merchantDao.selectOne(wrapper);
        if (Objects.isNull(merchant)) {
            throw new CrmebException("店铺信息异常");
        }
        return merchant;
    }

    @Override
    public MerchantEmployee findMerchantEmployee(String employeeNo) {
        LambdaQueryWrapper<MerchantEmployee> merchantEmployeeWrapper = new LambdaQueryWrapper<>();
        merchantEmployeeWrapper.eq(MerchantEmployee::getEmployeeNo, employeeNo);
        merchantEmployeeWrapper.eq(MerchantEmployee::getEnable, BooleanEnums.TRUE.getCode());
        merchantEmployeeWrapper.eq(MerchantEmployee::getDeleted, BooleanEnums.FALSE.getCode());
        return merchantEmployeeDao.selectOne(merchantEmployeeWrapper);
    }

    @Override
    public MerchantEmployee findEmployeeByUserId(Integer userId) {
        LambdaQueryWrapper<MerchantEmployee> merchantEmployeeWrapper = new LambdaQueryWrapper<>();
        merchantEmployeeWrapper.eq(MerchantEmployee::getUserId, userId);
        merchantEmployeeWrapper.eq(MerchantEmployee::getEnable, BooleanEnums.TRUE.getCode());
        merchantEmployeeWrapper.eq(MerchantEmployee::getDeleted, BooleanEnums.FALSE.getCode());
        return merchantEmployeeDao.selectOne(merchantEmployeeWrapper);
    }

    @Override
    public void disableMerchantEmployee(String employeeNo) {
        merchantEmployeeDao.disableMerchantEmployee(employeeNo);
    }
}
